import Api from './api'
import Address from './address'

/**
 *
 * @param page
 * @returns {Promise<{data: *, result: *}>}
 */
const getUserTask = async (page = 1) => {
  let url = Address.getUserTask() + Address.getPageParams('?', page)
  let res = await Api.netFetch(url)
  if (res && res.result) {
    let data = res.data.data
    data.forEach((item) => {
      let ex = {
        id: item.id,
        name: item.lesson_name,
        status: item.status,
        score: item.score
      }
      item.ex = ex
    })
    return {
      data: data,
      result: res.result,
    }
  }
}
/**
 * @param id
 * @returns {Promise<{data: *, result: *}>}
 */
const getTaskDesc = async (id) => {
  let url = Address.getUserTask() + `/${id}`
  let res = await Api.netFetch(url)
  if (res && res.result) {
    let data = res.data.data
    data.subjectItems.forEach((item) => {
      let list = []
      switch (item.type) {
        case 'judge':
          list = [
            { title: '正确', value: '0' },
            { title: '错误', value: '1' },
          ]
          break
        case 'radio':
        case 'checkbox':
          item.subjects_item.forEach((itemP,index)=>{
            list.push({
              title:itemP,
              value:index
            })
        })
          break
      }
      item.list = list
    })

    return {
      data: data,
      result: res.result,
    }
  }
}
/**
 * 提交作业
 * @param id
 * @param requestParams
 * @returns {Promise<{data: *, result: *}>}
 */
const submitTask = async (id,requestParams) => {
  let url = Address.getUserTask() + `/${id}`
  let res = await Api.netFetch(url, 'PUT', requestParams, true)
  let data = res.data
  return {
    data: data,
    result: res.result,
  }
}

export default {
  getUserTask,
  getTaskDesc,
  submitTask
}
